import 'dart:math';
import 'package:flutter/material.dart';

class DefineCircleProgress extends StatelessWidget {

  const DefineCircleProgress({Key? key, required this.circleRadius, required this.currentDegree}) : super(key: key);

  final double circleRadius;
  final double  currentDegree;

  @override
  Widget build(BuildContext context) {
     return Transform.rotate(
       //逆时针旋转90度  此处需要转化总长度(2 * pi)的比例  比如1/3
       angle: - 2 * pi / 4,
       child: CustomPaint(
            size: Size.fromRadius(circleRadius),
            painter: CircleProgressPainter(circleRadius: circleRadius, currentDegree: currentDegree),
       ),
     );
  }
}

class CircleProgressPainter extends CustomPainter {

  final double circleRadius;
  final double currentDegree;

  CircleProgressPainter({required this.circleRadius, required this.currentDegree});

  var backgroundPaint = Paint()
       ..color = Colors.red
       //..strokeCap = StrokeCap.butt  方头
       //..strokeCap = StrokeCap.round 圆头
       ..isAntiAlias = true
       ..strokeWidth = 5
       ..style = PaintingStyle.stroke;

  var progressPaint = Paint()
       ..color = Colors.lightBlue
       //..strokeCap = StrokeCap.butt   方头
       //..strokeCap = StrokeCap.round  圆头
       ..isAntiAlias = true
       ..strokeWidth = 5
       ..style = PaintingStyle.stroke;

  final List<Color> _colors = [
    Colors.lightBlue,
    Colors.lightBlueAccent,
    Colors.blue,
    Colors.blueAccent,
  ];

  @override
  void paint(Canvas canvas, Size size) {
      var rect = Rect.fromLTRB(0, 0, 2 * circleRadius, 2 * circleRadius);
      //起始点
      //var start = -pi * 2 * 90 / 360;
      //起始点设置为0，不然的话  SweepGradient扫描渐变达不到效果
      var start = 0.0;
      var end = pi * 2 * currentDegree / 360;
      //drawArc需要把角度换算成弧度
      canvas.drawArc(rect, start, pi * 2, false, backgroundPaint);
      var gradient = SweepGradient(
          colors: _colors,
          startAngle: start,
          endAngle: end
      );
      progressPaint.shader = gradient.createShader(rect);
      canvas.drawArc(rect, start, end, false, progressPaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
      return this != oldDelegate;
  }

}